<?php
	include 'php/config.php';
	session_start();
	if (!isset($_SESSION["id_utente"])) {
		header("Location: " . $ISSUER_HOST . "/login.php");
		exit();
	} else if (!$_SESSION["admin"]) {
		header("Location: " . $ISSUER_HOST . "/forbidden.html");
		exit();
	}
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF8">
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.2/themes/cupertino/jquery-ui.css" />
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.2/jquery-ui.js"></script>
<script src="js/utils.js"></script>
<script src="js/issue.js"></script>
 <script>
$(function() {
$( "#expires" ).datepicker({ minDate: 0, dateFormat: "yy-mm-dd" });
});
</script>
<title>Open Badges - Issue</title>
</head>
<body>
	<h1>Open Badges - Issue</h1>
	<div>
	<p>Da questa pagina è possibile assegnare una badge</p>
	</div>
	<?php
		// Connecting, selecting database
		$mysqli = new mysqli($MYSQL_HOST, $MYSQL_USERNAME, $MYSQL_PASSWORD, $MYSQL_DB_NAME);
		if ($mysqli->connect_errno) {
			echo "<p>Failed to connect to MySQL: " . $mysqli->connect_error . "<p>";
		} else {
			$queries_ok = TRUE;
			$badge_query = "SELECT `badgeId`, `nome` FROM `badge`";
			$badges = $mysqli->query($badge_query);
			if (!$badges) {
				echo "<p>Failed to run query " . $badge_query . ": (" . $mysqli->errno . ") " . 
					$mysqli->error . "<p>";
				$queries_ok = FALSE;
			}
			
			$user_query = "SELECT `userId`, `nome`, `cognome`, `email` FROM `user` WHERE `admin`=FALSE";
			$users = $mysqli->query($user_query);
			if (!$users) {
				echo "<p>Failed to run query " . $user_query . ": (" . $mysqli->errno . ") " .
						$mysqli->error . "<p>";
				$queries_ok = FALSE;
			}
			
			if ($queries_ok && $badges->num_rows && $users->num_rows) {
		
	?>
	<form action="php/scripts/create_assertion.php" method="post">
	<div>
	<label>Seleziona una badge</label>
	<select id="badge" name="badge" onchange="validateIssueForm()">
		<option value="">Scegli una badge</option>
	<?php
		while ($row = $badges->fetch_assoc()) {
    		echo "<option value=\"" . $row['badgeId'] . "#" . $row['nome'] . "\">" . $row['nome'] . "</option>";    
		}
		mysqli_free_result($badges);
	?>
	</select>
	</div>
	<div>
	<label>E-mail earner</label>
	<select id="user" name="user" onchange="validateIssueForm()">
		<option value="">Scegli un utente</option>
	<?php
		while ($row = $users->fetch_assoc()) {
    		echo "<option value=" . $row['userId'] . "#" . $row['email'] . ">" . 
    			$row['nome'] . " " . $row['cognome'] . " (" . $row['email'] . ")</option>";    
		}
		mysqli_free_result($users);		
	?>
	</select>
	</div>
	<div>
	<label>Evidence</label>
	<input id="evidenceUrl" name="evidenceUrl" type="text" maxLength="100" />
	</div>
	<div>
	<label>Expires</label>
	<input id="expires" name="expires" type="text" />
	</div>
	<div id="message"></div>
	<input id="issue" type="submit" value="Assegna" disabled="disabled" />
	</form>
	<?php
			} else { // end if (queries_ok && ... )
				if (!$badges->num_rows) {
					echo "<p>Non ci sono badges da assegnare</p>";
					echo "<p>Crea una badge: <a href=\"insert_badge_page.html\">Crea</a></p>";
				}
				
				if (!$users->num_rows) {
					echo "<p>Non ci sono utenti a cui assegnare badges</p>";
					echo "<p>Crea un nuovo utente: <a href=\"new_user_page.html\">Utente</a></p>";
				}
			}
			
			$mysqli->close();
		} // end else (if connection to db was ok) 
	?>
	<div>
	<p>Torna alla <a href="index.html">Home</a></p>
	</div>
</body>
</html>